//
//  CustomChartView.m
//  iMoc
//
//  Created by 微我网络 on 15/1/16.
//  Copyright (c) 2015年 lamcelot. All rights reserved.
//

#import "CustomChartView.h"


@interface CustomChartView ()

@property (nonatomic,strong) UIScrollView *scroll;

@property (nonatomic,strong) UILabel *title;
@property (nonatomic,strong) UILabel *label;
@property (nonatomic,strong) CALayer *line0;
@property (nonatomic,strong) CALayer *line1;

@property (nonatomic,strong) NSMutableArray *xLabels;

@end

@implementation CustomChartView

- (instancetype)initWithFrame:(CGRect)frame style:(ChartViewStyle)style {
    
    if (self = [super initWithFrame:frame]) {
        
        self.image = [UIImage imageNamed:@"chart_amount"];
        self.contentMode = UIViewContentModeScaleAspectFill;
        self.layer.cornerRadius = 10;
        self.layer.masksToBounds = YES;
        self.userInteractionEnabled = YES;
//        self.layer.borderWidth = 0.3;
//        self.layer.borderColor = [UIColor whiteColor].CGColor;
        self.frame = frame;
        
        _xLabels = [NSMutableArray array];
        
        _style = style;
        
        _line0 = [CALayer layer];
        _line0.backgroundColor = [UIColor grayColor].CGColor;
        _line0.frame = CGRectMake(20, 30, frame.size.width-40, 1);
        [self.layer addSublayer:_line0];
        
        _line1 = [CALayer layer];
        _line1.backgroundColor = [UIColor grayColor].CGColor;
        _line1.frame = CGRectMake(20, frame.size.height-27, frame.size.width-40, 1);
        [self.layer addSublayer:_line1];
        
        _title = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, frame.size.width/2, 30)];
        _title.font = [UIFont boldSystemFontOfSize:20];
        _title.textColor = [UIColor whiteColor];
        [self addSubview:_title];
        
        _label = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width-120, 0, 100, 30)];
        _label.font = [UIFont systemFontOfSize:18];
        _label.textColor = [UIColor whiteColor];
        _label.textAlignment = NSTextAlignmentRight;
        [self addSubview:_label];
        
        _scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 30, frame.size.width-40, frame.size.height)];
//        _scroll.contentSize = CGSizeMake(frame.size.width*2-40, frame.size.height-57);
        _scroll.showsHorizontalScrollIndicator = YES;
        _scroll.userInteractionEnabled = YES;
        _scroll.scrollEnabled = YES;
        _scroll.backgroundColor = [UIColor clearColor];
        [self addSubview:_scroll];
        
        _graph = [[MPGraphView alloc] init];
        _graph.waitToUpdate = YES;
        _graph.curved = YES;
        _graph.showTargetLine = NO;
        _graph.showPoints = YES;
        _graph.graphColor = [UIColor clearColor];
        [self setStyle:style];
        [_scroll addSubview:_graph];
        
//        NSArray *xLabels = @[@"Sun",@"Mon",@"Tue",@"Wed",@"Thu",@"Fri",@"Sat"];
//        [self setXLabelsWithArray:xLabels];
    }
    return self;
}

- (void)setValue:(NSString *)value {
    
    if (_style == ChartViewStyleAmount) {
        NSString *full = [NSString stringWithFormat:@"%@步",value];
        NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:full];
        [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:17],
                                   NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(0,value.length)];
        [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:9],
                                   NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(full.length-1, 1)];
        _label.attributedText = attribute;
    }
    else {
        NSString *hour   = [NSString stringWithFormat:@"%ld",[value integerValue]/60];
        NSString *minute = [NSString stringWithFormat:@"%ld",[value integerValue]%60];
        
        NSString *full = [NSString stringWithFormat:@"%@时%@分",hour,minute];
        NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:full];
        [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:17],
                                   NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(0,hour.length)];
        [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:9],
                                   NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(hour.length, 1)];
        [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:17],
                                   NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(hour.length+1, minute.length)];
        [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:9],
                                   NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(full.length-1, 1)];
        _label.attributedText = attribute;
    }
}

- (BOOL)showPoints {
    
    return _graph.showPoints;
}

- (void)setShowPoints:(BOOL)showPoints {
    
    _graph.showPoints = showPoints;
}

- (NSArray *)values {
    
    return _graph.values;
}

- (void)setValues:(NSArray *)values {
    
    _graph.values = values;
    if (_graph.values.count*50 > _scroll.frame.size.width) {
        [_scroll setContentSize:CGSizeMake(50*_graph.values.count, _scroll.frame.size.height)];
        [_graph setFrame:CGRectMake(0, 0, _scroll.contentSize.width, _scroll.contentSize.height-57)];
    }
    else {
        [_scroll setContentSize:CGSizeMake(_scroll.frame.size.width, _scroll.frame.size.height)];
        [_graph setFrame:CGRectMake(0, 0, _scroll.contentSize.width, _scroll.contentSize.height-57)];
    }
    
    [_graph animate];
}

- (void)setStyle:(ChartViewStyle)style {
    
    _style = style;
    if (style == ChartViewStyleAmount) {
        _title.text = @"步数";
        self.image = [UIImage imageNamed:@"chart_amount"];
//        _line0.backgroundColor = [UIColor colorWithRed:225/255.f green:182/255.f blue:171/255.f alpha:.7f].CGColor;
//        _line1.backgroundColor = [UIColor colorWithRed:225/255.f green:182/255.f blue:171/255.f alpha:.5f].CGColor;
        _line0.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.5].CGColor;
        _line1.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.3].CGColor;
        _graph.fillColors = @[[UIColor colorWithWhite:0.2 alpha:0.7],
                              [UIColor colorWithWhite:0.2 alpha:0.7]];
        ((CAShapeLayer *)_graph.layer).fillColor = [UIColor colorWithWhite:0.2 alpha:0.7].CGColor;
        _graph.graphColor = [UIColor colorWithWhite:0.2 alpha:0.7];
    }
    else if (style == ChartViewStyleSleep) {
        _title.text = @"总睡眠";
        self.image = [UIImage imageNamed:@"chart_sleep"];
        _line0.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.5].CGColor;
        _line1.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.3].CGColor;
        _graph.fillColors = @[[[UIColor cyanColor] colorWithAlphaComponent:0.1],
                              [[UIColor cyanColor] colorWithAlphaComponent:0.1]];
        ((CAShapeLayer *)_graph.layer).fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.1].CGColor;
        _graph.graphColor = [[UIColor cyanColor] colorWithAlphaComponent:0.1];
    }
}

- (void)setXTitle:(NSMutableArray *)xTitle {
    
    _xTitle = [NSMutableArray arrayWithArray:xTitle];
    
    for (UILabel *label in _xLabels) {
        [label removeFromSuperview];
    }
    [_xLabels removeAllObjects];
    
    for (int i = 0; i < _xTitle.count; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];
        label.text = _xTitle[i];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:12];
//        label.backgroundColor = [UIColor redColor];
        label.textColor = [UIColor colorWithWhite:0.8 alpha:0.8];
//        CGFloat space = (self.frame.size.width-80)/MAX((_xTitle.count-1), 1);
        
        CGFloat space = (_scroll.contentSize.width-40)/MAX((_xTitle.count-1), 1);
        label.center = CGPointMake(20+(space)*i, CGRectGetMaxY(_graph.frame)+10);
        [_scroll addSubview:label];
        [_xLabels addObject:label];
    }
    
//    NSAssert(xTitle.count == _graph.values.count, @"error:The count must be equaled");
//    if (xTitle.count != _graph.values.count) {
//        return;
//    }
//    else if(xTitle.count == _graph.values.count){
//        
//        
//    }
}

- (void)setTargetValue:(CGFloat)targetValue {
    
    _targetValue = targetValue;
    _graph.targetValue = targetValue;
}

- (void)drawLineOn:(UIView *)view {
    
    UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(40, 45, 240, 20)];
    [view addSubview:imageView1];
    UIGraphicsBeginImageContext(imageView1.frame.size);
    [imageView1.image drawInRect:CGRectMake(0, 0, imageView1.frame.size.width, imageView1.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGFloat lengths[] = {1,1};
    CGContextRef line = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(line, [UIColor blackColor].CGColor);
    CGContextSetLineDash(line, 0, lengths, 2);
    CGContextMoveToPoint(line, 0.0, 20.0);
    CGContextAddLineToPoint(line, 310.0, 20.0);
    CGContextStrokePath(line);
    imageView1.image = UIGraphicsGetImageFromCurrentImageContext();
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
